
//_______________________________________________________________________________________________________________
/****************************************************************************************************************/
/*                                                                                                              */
/*                     B I N A R Y    S E M A P H O R E S                                                       */
/*                                                                                                              */
/****************************************************************************************************************/

#ifndef _OS_SETBIT_BINSEM
#define _OS_SETBIT_BINSEM(binsem)   {OS_BSems[(binsem) >> _OST_INT_SHIFT] |= (1 << ((binsem) & _OST_INT_MASK));}
#endif
    
#ifndef _OS_CLRBIT_BINSEM
#define _OS_CLRBIT_BINSEM(binsem)   {OS_BSems[(binsem) >> _OST_INT_SHIFT] &= ~(1 << ((binsem) & _OST_INT_MASK));}
#endif

#ifndef _OS_TGLBIT_BINSEM
#define _OS_TGLBIT_BINSEM(binsem)   {OS_BSems[(binsem) >> _OST_INT_SHIFT] ^= (1 << ((binsem) & _OST_INT_MASK));}
#endif

    

//------------------------------------------------------------------------------
// Signal binary semaphore
#define OS_SetBSem(binsem)        _OS_SETBIT_BINSEM(binsem)
#define OS_SignalBSem(binsem)     OS_SetBSem(binsem)

//------------------------------------------------------------------------------
// Reset binary semaphore
#define OS_ResetBSem(binsem)     _OS_CLRBIT_BINSEM(binsem)

//------------------------------------------------------------------------------
// Check for binary semaphore is set
#define OS_CheckBSem(binsem)     (OS_BSems[(binsem) >> _OST_INT_SHIFT] & (1 << ((binsem) & _OST_INT_MASK)))

//------------------------------------------------------------------------------
// Inverce semaphore's value
#define OS_SwitchBSem(binsem)    _OS_TGLBIT_BINSEM(binsem)
    
//------------------------------------------------------------------------------
// Wait binary semaphore

#define OS_WaitBSem(binsem)                                           \
    {                                                                 \
        _OS_WAIT_EVENT(OS_CheckBSem(binsem));                         \
        OS_ResetBSem(binsem);                                         \
    }

//-------------------------------------------------------------------------------
// Wait binary semaphore. Exit if timeout expired

#define OS_WaitBSemTimeout(binsem, timeout)                           \
    {                                                                 \
        _OS_WAIT_EVENT_TIMEOUT(OS_CheckBSem(binsem), timeout);        \
        OS_ResetBSem(binsem);                                         \
    }





    
